#!/bin/bash
###############################################################################
#
# Title: Pre Install Script for Salt Installation
# Authors: Shane Lee
# Date: December 2015
#
# Description: This script stops the salt minion service before attempting to
#              install Salt on macOS. It also removes the /opt/salt/bin
#              directory, symlink to salt-config, and salt from paths.d.
#
# Requirements:
#    - None
#
# Usage:
#     This script is run as a part of the macOS Salt Installation
#
###############################################################################

###############################################################################
# Define Variables
###############################################################################
# Get Minor Version
OSX_VERSION=$(sw_vers | grep ProductVersion | cut -f 2 -d: | tr -d '[:space:]')
MINOR=$(echo ${OSX_VERSION} | cut -f 2 -d.)
# Path Variables
INSTALL_DIR="/opt/salt"
BIN_DIR="$INSTALL_DIR/bin"
CONFIG_DIR="/etc/salt"
TEMP_DIR="/tmp"
SBIN_DIR="/usr/local/sbin"

###############################################################################
# Set up logging and error handling
###############################################################################
echo "Preinstall started on:" > "$TEMP_DIR/preinstall.txt"
date "+%Y/%m/%d %H:%m:%S" >> "$TEMP_DIR/preinstall.txt"
trap 'quit_on_error $LINENO $BASH_COMMAND' ERR

quit_on_error() {
    echo "$(basename $0) caught error on line : $1 command was: $2" >> "$TEMP_DIR/preinstall.txt"
    exit -1
}

OSX_VERSION=$(sw_vers | grep ProductVersion | cut -f 2 -d: | tr -d '[:space:]')
MINOR=$(echo ${OSX_VERSION} | cut -f 2 -d.)

###############################################################################
# Stop the service
###############################################################################
stop_service_maverick() {
    echo "Service: Using old (< 10.10) launchctl interface" >> "$TEMP_DIR/preinstall.txt"
    if /bin/launchctl list "com.saltstack.salt.minion" &> /dev/null; then
        echo "Service: Unloading minion..." >> "$TEMP_DIR/preinstall.txt"
        launchctl unload -w /Library/LaunchDaemons/com.saltstack.salt.minion.plist
        echo "Service: Unloaded Successfully" >> "$TEMP_DIR/preinstall.txt"
    fi
    if /bin/launchctl list "com.saltstack.salt.master" &> /dev/null; then
        echo "Service: Unloading master..." >> "$TEMP_DIR/preinstall.txt"
        launchctl unload -w /Library/LaunchDaemons/com.saltstack.salt.master.plist
        echo "Service: Unloaded Successfully" >> "$TEMP_DIR/preinstall.txt"
    fi
    if /bin/launchctl list "com.saltstack.salt.syndic" &> /dev/null; then
        echo "Service: Unloading syndic..." >> "$TEMP_DIR/preinstall.txt"
        launchctl unload -w /Library/LaunchDaemons/com.saltstack.salt.syndic.plist
        echo "Service: Unloaded Successfully" >> "$TEMP_DIR/preinstall.txt"
    fi
    if /bin/launchctl list "com.saltstack.salt.api" &> /dev/null; then
        echo "Service: Unloading api..." >> "$TEMP_DIR/preinstall.txt"
        launchctl unload -w /Library/LaunchDaemons/com.saltstack.salt.api.plist
        echo "Service: Unloaded Successfully" >> "$TEMP_DIR/preinstall.txt"
    fi
}

stop_service_yosemite_and_later() {
    echo "Service: Using new (>= 10.10) launchctl interface" >> "$TEMP_DIR/preinstall.txt"
    if /bin/launchctl list "com.saltstack.salt.minion" &> /dev/null; then
        echo "Service: Stopping minion..." >> "$TEMP_DIR/preinstall.txt"
        launchctl disable system/com.saltstack.salt.minion
        launchctl bootout system /Library/LaunchDaemons/com.saltstack.salt.minion.plist
        echo "Service: Stopped Successfully" >> "$TEMP_DIR/preinstall.txt"
    fi
    if /bin/launchctl list "com.saltstack.salt.master" &> /dev/null; then
        echo "Service: Stopping master..." >> "$TEMP_DIR/preinstall.txt"
        launchctl disable system/com.saltstack.salt.master
        launchctl bootout system /Library/LaunchDaemons/com.saltstack.salt.master.plist
        echo "Service: Stopped Successfully" >> "$TEMP_DIR/preinstall.txt"
    fi
    if /bin/launchctl list "com.saltstack.salt.syndic" &> /dev/null; then
        echo "Service: Stopping syndic..." >> "$TEMP_DIR/preinstall.txt"
        launchctl disable system/com.saltstack.salt.syndic
        launchctl bootout system /Library/LaunchDaemons/com.saltstack.salt.syndic.plist
        echo "Service: Stopped Successfully" >> "$TEMP_DIR/preinstall.txt"
    fi
    if /bin/launchctl list "com.saltstack.salt.api" &> /dev/null; then
        echo "Service: Stopping api..." >> "$TEMP_DIR/preinstall.txt"
        launchctl disable system/com.saltstack.salt.api
        launchctl bootout system /Library/LaunchDaemons/com.saltstack.salt.api.plist
        echo "Service: Stopped Successfully" >> "$TEMP_DIR/preinstall.txt"
    fi
}

echo "Service: Configuring..." >> "$TEMP_DIR/preinstall.txt"
case $MINOR in
        9 )
                stop_service_maverick;
                ;;
        * )
                stop_service_yosemite_and_later;
                ;;
esac
echo "Service: Configured Successfully" >> "$TEMP_DIR/preinstall.txt"

###############################################################################
# Remove the Symlink to salt-config.sh
###############################################################################
if [ -L "$SBIN_DIR/salt-config" ]; then
    echo "Cleanup: Removing Symlink $BIN_DIR/salt-config" >> "$TEMP_DIR/preinstall.txt"
    rm "$SBIN_DIR/salt-config"
    echo "Cleanup: Removed Successfully" >> "$TEMP_DIR/preinstall.txt"
fi

###############################################################################
# Remove the $INSTALL_DIR directory
###############################################################################
if [ -d "$INSTALL_DIR" ]; then
    echo "Cleanup: Removing $INSTALL_DIR" >> "$TEMP_DIR/preinstall.txt"
    rm -rf "$INSTALL_DIR"
    echo "Cleanup: Removed Successfully" >> "$TEMP_DIR/preinstall.txt"
fi

###############################################################################
# Remove the salt from the paths.d
###############################################################################
if [ -f "/etc/paths.d/salt" ]; then
    echo "Path: Removing salt from the path..." >> "$TEMP_DIR/preinstall.txt"
    rm "/etc/paths.d/salt"
    echo "Path: Removed Successfully" >> "$TEMP_DIR/preinstall.txt"
fi

echo "Preinstall Completed Successfully on:" >> "$TEMP_DIR/preinstall.txt"
date "+%Y/%m/%d %H:%m:%S" >> "$TEMP_DIR/preinstall.txt"

exit 0
